<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">螃蟹兑换系统登录</h3>
      <el-form-item prop="username" class="form-item">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号"> </el-input>
        <i class="el-icon-user user-icon"></i>
      </el-form-item>
      <el-form-item prop="password" class="form-item">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
        </el-input>
        <i class="el-icon-lock lock-icon"></i>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button :loading="loading" size="medium" type="primary" style="width: 100%" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
      <!-- <div class="pwd-tip">默认账号 密码：admin 123</div> -->
    </el-form>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { generateRandomId } from '@/utils/index';
import { login } from '@/api/index/index';

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
      },
      loading: false,
    };
  },
  computed: {
    ...mapState({
      defaultAccountInfo: (state) => state.defaultAccountInfo,
    }),
  },
  watch: {},
  created() {
    if (this.$store.state.token) {
      this.$router.replace({ path: '/app/coupon' });
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          login({
            userName: this.loginForm.username.trim(),
            userPassword: this.loginForm.password.trim(),
          })
            .then((res) => {
              console.log(res);
              this.loading = false;
              if (res.data.code === '000000') {
                this.$store.commit('setToken', generateRandomId());
                this.$store.commit('updateLoginTime');
                this.$store.commit('updateAccountInfo', {
                  account: this.loginForm.username.trim(),
                  password: this.loginForm.password.trim(),
                  userId: res.data.resData.userInfoId,
                });
              } else {
                this.$message.error('账号或密码错误!');
              }
              this.$router.push({ path: '/app/coupon' });
            })
            .catch((err) => {
              console.log(err);
              this.$message.error('登录失败!');
              this.loading = false;
            });
          // 验证账号密码是否正确
          // const { username, password } = this.loginForm;
          // const storeAccount = this.defaultAccountInfo.account;
          // const storePwd = this.defaultAccountInfo.password;
          // 验证输入的账号密码与store中的是否一致
          // 如果一致，登录成功，更新当前登录时间戳，跳转到首页
          // 如果不一致，提示账号密码错误
          // if (username.trim() === storeAccount && password.trim() === storePwd) {
          //   this.loading = false;
          //   this.$store.commit('setToken', generateRandomId());
          //   this.$store.commit('updateLoginTime');
          //   this.$router.push({ path: '/app/coupon' });
          // } else {
          //   this.loading = false;
          //   this.$message.error('账号或密码错误');
          // }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  overflow: hidden;
  display: flex;
  justify-content: center;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    overflow: hidden;
    .title {
      font-size: 26px;
      color: #eee;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: 700;
    }
    .form-item::v-deep {
      position: relative;
      margin-bottom: 30px;
      .user-icon {
        position: absolute;
        top: 50%;
        transform: translateY(-52%);
        left: 10px;
        color: #c0c4cc;
        font-size: 18px;
      }
      .lock-icon {
        position: absolute;
        top: 50%;
        transform: translateY(-48%);
        left: 10px;
        color: #c0c4cc;
        font-size: 18px;
      }
      .el-input__inner {
        padding: 0 40px;
      }
    }
    .pwd-tip {
      color: #c0c4cc;
      font-size: 12px;
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>
